package com.highglass.videoplayer;

import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.util.Log;

import com.hg.library.graph.Fonts;
import com.hg.library.utils.SystemDevState;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.LruMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import java.util.List;

/**
 * Created by Nick on 2015/11/27.
 */
public class SoftApplication extends Application {
    public static SoftApplication softApplication;
    @Override
    public void onCreate() {
        softApplication = this;
        super.onCreate();
//        CrashHandler.getCrashHandler().init(getApplicationContext());
        ImageLoaderConfiguration.Builder config = new ImageLoaderConfiguration.Builder(getApplicationContext());
        DisplayImageOptions.Builder options = new DisplayImageOptions.Builder();
        config.memoryCacheSize(100 * 1024 * 1024);
        config.diskCacheFileCount(10000);
        config.memoryCache(new LruMemoryCache(100 * 1024 * 1024));
        config.diskCacheFileNameGenerator(new Md5FileNameGenerator());
        options.cacheInMemory(true);                           //启用内存缓存
        options.cacheOnDisk(true);                              //启用外存缓存
        options.considerExifParams(true);
        options.bitmapConfig(Bitmap.Config.RGB_565);
        options.delayBeforeLoading(100);
        options.showImageOnLoading(R.drawable.ic_launcher);
        DisplayImageOptions options1 = options.build();
        config.defaultDisplayImageOptions(options1);
        ImageLoader.getInstance().init(config.build());
        Fonts.init(this);
    }

    public int getAppVersionCode() {
        PackageManager localPackageManager = getPackageManager();
        try {
            int i = localPackageManager.getPackageInfo(getPackageName(), 0).versionCode;
            return i;
        } catch (PackageManager.NameNotFoundException localNameNotFoundException) {
            localNameNotFoundException.printStackTrace();
        }
        return 0;
    }

    @Override
    public void onTrimMemory(int level) {
        ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
        activityManager.getMemoryInfo(memoryInfo);
        //目前可用内存
        long beforeLarge = memoryInfo.availMem / 1024 / 1024;
        if (beforeLarge < 512) {
            List<ActivityManager.RunningAppProcessInfo> runningAppProcesses = activityManager.getRunningAppProcesses();
            for (int i = 0; i < runningAppProcesses.size(); i++) {
                ActivityManager.RunningAppProcessInfo runningAppProcessInfo = runningAppProcesses.get(i);
                //判断当进程等级，值越小，等级越高
                if (runningAppProcessInfo.importance > ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                    String[] pkgList = runningAppProcessInfo.pkgList;
                    for (int j = 0; j < pkgList.length; j++) {
                        Log.e("fuck you kill",pkgList[j]);
                        if(isNotSystemApp(pkgList[j]))
                        activityManager.killBackgroundProcesses(pkgList[j]);
                    }
                }
            }
        }
        super.onTrimMemory(level);
    }
    private boolean isNotSystemApp(String pageName){
         if(!pageName.equals("com.hg.killer_whale.file_manager")&&!pageName.equals("com.hg.video")){
             return false;
         }
        return true;
    }
}
